package com.ts.main.mini.exception;


/**
 * @author lk
 */
public class CommonException extends RuntimeException implements CommonError {

    private CommonError commonError;

    private Integer errCode = -1;
    private String errMsg;
    private String serviceName;

    public CommonException() {
        super();
    }

    public CommonException(CommonError commonError) {
        super();
        this.commonError = commonError;
    }

    public CommonException(CommonError commonError, String errMsg) {
        super();
        this.commonError = commonError;
        this.commonError.setErrMsg(errMsg);
    }

    public CommonException(Integer errCode, String errMsg) {
        super();
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public CommonException(String errMsg) {
        super();
        this.errMsg = errMsg;
    }

    public CommonException(Integer errCode, String errMsg, String serviceName) {
        super();
        this.errCode = errCode;
        this.errMsg = errMsg;
        this.serviceName = serviceName;
    }

    @Override
    public int getErrCode() {
        if (this.commonError != null) {
            this.errCode = this.commonError.getErrCode();
        }
        return this.errCode;
    }

    @Override
    public String getErrMsg() {
        if (this.errMsg == null && this.commonError != null) {
            this.errMsg = this.commonError.getErrMsg();
        }
        return this.errMsg;
    }

    @Override
    public String getServiceName() {
        if (this.serviceName == null && this.commonError != null) {
            this.serviceName = this.commonError.getServiceName();
        }
        return this.serviceName;
    }

    @Override
    public CommonError setErrMsg(String errMsg) {
        if(this.commonError != null){
            this.commonError.setErrMsg(errMsg);
        }
        return this.commonError;
    }

    //toString
    @Override
    public String toString() {
        return "CommonException{" +
          "commonError=" + commonError +
          ", errCode=" + errCode +
          ", errMsg='" + errMsg + '\'' +
          ", serviceName='" + serviceName + '\'' +
          '}';
    }
}
